package 力扣._240_搜索二维矩阵2;

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        //从第一行向右顺序遍历，若等于则返回，若小于则二分遍历列，若大于则false
        for (int j = 0; j < matrix[0].length; j++) {
            if (matrix[0][j] == target)
                return true;
            else if (matrix[0][j] > target)
                return false;
            else {
                int l = 1, h = matrix.length - 1, mid;
                while (l <= h) {
                    mid = (l + h) / 2;
                    if (matrix[mid][j] == target)
                        return true;
                    else if (matrix[mid][j] > target)
                        h = mid - 1;
                    else
                        l = mid + 1;
                }
            }
        }
        return false;
    }
}
